﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using RaytracerLib.MathLib;
using TomanuExtensions;
using RaytracerLib.Attenuations;
using System.Collections.Generic;

namespace Raytracer.PropertyGridDecorators
{
    public class RendererConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext a_context,
            Type a_source_type)
        {
            return base.CanConvertFrom(a_context, a_source_type);
        }

        public override bool CanConvertTo(ITypeDescriptorContext a_context,
            Type a_destination_type)
        {
            return base.CanConvertTo(a_context, a_destination_type);
        }

        public override object ConvertFrom(ITypeDescriptorContext a_context,
            CultureInfo a_culture, object a_value)
        {
            return base.ConvertFrom(a_context, a_culture, a_value);
        }

        public override object ConvertTo(ITypeDescriptorContext a_context,
            CultureInfo a_culture, object a_value, Type a_destination_type)
        {
            if (a_destination_type == typeof(string))
            {
                if (a_value is SamplesVisualizerRendererProperties)
                {
                    return "Scale: " + (a_value as SamplesVisualizerRendererProperties).Scale;
                }
                else if (a_value is SamplesPeriodogramRendererProperties)
                {
                    var props = a_value as SamplesPeriodogramRendererProperties;

                    return String.Format("repeats: {0}, scale samples: {1}, scale values: {2}", 
                        props.Repeats, props.ScaleSamples, props.ScaleValues);
                }

                return "-";
            }

            return base.ConvertTo(a_context, a_culture, a_value, a_destination_type);
        }

        public override bool GetPropertiesSupported(ITypeDescriptorContext a_context)
        {
            return true;
        }

        public override PropertyDescriptorCollection GetProperties(
            ITypeDescriptorContext a_context, object a_value, Attribute[] a_attributes)
        {
            if ((a_value is SamplesPeriodogramRendererProperties) || (a_value is SamplesVisualizerRendererProperties))
                return TypeDescriptor.GetProperties(a_value, a_attributes);
            else
                return base.GetProperties(a_context, a_value, a_attributes);
        }
    }
}